package com.egoo.ticket.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egoo.ticket.server.dao.MsgNotifySettingMapper;
import com.egoo.ticket.server.dao.MsgSettingRelationMapper;
import com.egoo.ticket.server.pojo.dto.MsgNotifySettingDto;
import com.egoo.ticket.server.pojo.entity.MsgNotifySetting;
import com.egoo.ticket.server.pojo.entity.MsgSettingRelation;
import com.egoo.ticket.server.pojo.vo.MsgNotifySettingVo;
import com.egoo.ticket.server.service.IMsgNotifySettingService;
import com.egoo.ticket.server.service.IMsgSettingRelationService;
import com.egoo.ticket.server.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author donny
 * @since 2020-10-29
 */
@Service
@Log4j2
//@Transactional(rollbackFor = Exception.class)
public class MsgNotifySettingServiceImpl extends ServiceImpl<MsgNotifySettingMapper, MsgNotifySetting> implements IMsgNotifySettingService {

	@Autowired
	private MsgNotifySettingMapper msgNotifySettingMapper;
	@Autowired
	private UserService userService;
	@Autowired
	MsgSettingRelationMapper msgSettingRelationMapper;
	@Autowired
	IMsgSettingRelationService iMsgSettingRelationService;



	/**
	 * @Author: Donny_dong
	 * @Description: 新增消息通知设置
	 * @Param:
	 * @return:
	 * @Date: 2020/10/30
	 */
	@Override
	public boolean insertMsgNotify(MsgNotifySettingDto msgNotifySettingDto) {

		String userId = msgNotifySettingDto.getUserId();
		String userName = userService.selectNameById(userId);
		String tenantId = msgNotifySettingDto.getTenantId();
		Long workOrderTypeId = msgNotifySettingDto.getWorkOrderTypeId();
		String notifyType = msgNotifySettingDto.getNotifyType();
		List<MsgSettingRelation> msgSettingRelations = msgNotifySettingDto.getMsgSettingRelations();
		MsgNotifySetting msgNotifySetting = new MsgNotifySetting();
		Date date = new Date();
		msgNotifySetting.setWorkOrderTypeId(workOrderTypeId)
						.setNotifyType(notifyType)
						.setStatus(1)
						.setCreator(userId)
						.setCreateTime(date)
						.setUpdaterName(userId)
						.setUpdateTime(date)
						.setTenantId(tenantId);
		int insert = msgNotifySettingMapper.insert(msgNotifySetting);
		if (insert>0) {
			msgSettingRelations = msgSettingRelations.stream().map(msgSettingRelation -> msgSettingRelation.setMsgNotifyId(msgNotifySetting.getId()).setTenantId(tenantId)).collect(Collectors.toList());
			log.info("新增时获取的设置条件为:"+msgSettingRelations);
//			Integer integer = msgSettingRelationMapper.batchInsert(msgSettingRelations);
			boolean b = iMsgSettingRelationService.saveBatch(msgSettingRelations);
			log.info("消息设置关系插入结果"+b);
			if (b){
				return true;
			}
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return false;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 修改消息通知设置
	 * @Param:
	 * @return:
	 * @Date: 2020/10/30
	 */
	@Override
	public boolean updateNotifySetting(MsgNotifySettingDto msgNotifySettingDto) {
		String userId = msgNotifySettingDto.getUserId();
		String userName = userService.selectNameById(userId);
		String tenantId = msgNotifySettingDto.getTenantId();
		Long workOrderTypeId = msgNotifySettingDto.getWorkOrderTypeId();
		String notifyType = msgNotifySettingDto.getNotifyType();
		Integer id = msgNotifySettingDto.getId();
		List<MsgSettingRelation> msgSettingRelations = msgNotifySettingDto.getMsgSettingRelations();

		MsgNotifySetting msgNotifySetting = new MsgNotifySetting();
		QueryWrapper<MsgNotifySetting> notifyQueryWrapper = new QueryWrapper<>();
		notifyQueryWrapper.lambda().eq(MsgNotifySetting::getId,id);
		Date date = new Date();
		msgNotifySetting.setWorkOrderTypeId(workOrderTypeId)
						.setNotifyType(notifyType)
						.setUpdateTime(date)
						.setUpdaterName(userId);
		int update = msgNotifySettingMapper.update(msgNotifySetting, notifyQueryWrapper);
		if (update>0){
			QueryWrapper<MsgSettingRelation> relationQueryWrapper = new QueryWrapper<>();
			relationQueryWrapper.lambda().eq(MsgSettingRelation::getMsgNotifyId,id);

			int delete = msgSettingRelationMapper.delete(relationQueryWrapper);
			if (delete <= 0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return false;
			}
			msgSettingRelations = msgSettingRelations.stream().map(msgSettingRelation ->msgSettingRelation.setMsgNotifyId(id).setTenantId(tenantId)).collect(Collectors.toList());
			boolean b = iMsgSettingRelationService.saveBatch(msgSettingRelations);
			if (b){
				return true;
			}
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return false;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 获取列表（列表搜索）
	 * @Param:
	 * @return:
	 * @Date: 2020/10/30
	 */
	@Override
	public List<MsgNotifySettingVo> getList(MsgNotifySettingDto msgNotifySettingDto) {
		String tenantId = msgNotifySettingDto.getTenantId();
		String searchContent = msgNotifySettingDto.getSearchContent();
		Integer pageNumber = msgNotifySettingDto.getPageNumber();
		Integer pageSize = msgNotifySettingDto.getPageSize();
//		Page<MsgNotifySetting> msgNotifySettingPage = new Page<>(pageNumber,pageSize);
//		QueryWrapper<MsgNotifySetting> queryWrapper = new QueryWrapper<>();
//		if (StringUtils.isEmpty(searchContent)) {
//			queryWrapper.lambda().eq(MsgNotifySetting::getTenantId,tenantId);
//		}else {
//			queryWrapper.lambda().eq(MsgNotifySetting::getTenantId,tenantId);
//		}
//		IPage<MsgNotifySetting> msgNotifySettingIPage = msgNotifySettingMapper.selectPage(msgNotifySettingPage, queryWrapper);
		PageHelper.startPage(pageNumber,pageSize);
		List<MsgNotifySettingVo> list = msgNotifySettingMapper.getList(msgNotifySettingDto);

		return list;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 根据id查询
	 * @Param:
	 * @return:
	 * @Date: 2020/10/30
	 */
	@Override
	public MsgNotifySettingDto byId(Integer id) {
		MsgNotifySettingDto msgNotifySettingDto = new MsgNotifySettingDto();
		MsgNotifySetting msgNotifySetting = msgNotifySettingMapper.selectById(id);
		if (ObjectUtils.isEmpty(msgNotifySetting)){
			return msgNotifySettingDto;
		}
		QueryWrapper<MsgSettingRelation> relationQueryWrapper = new QueryWrapper<>();
		relationQueryWrapper.lambda().eq(MsgSettingRelation::getMsgNotifyId,id);
		List<MsgSettingRelation> msgSettingRelations = msgSettingRelationMapper.selectList(relationQueryWrapper);
		msgNotifySettingDto.setId(id)
							.setWorkOrderTypeId(msgNotifySetting.getWorkOrderTypeId())
							.setNotifyType(msgNotifySetting.getNotifyType())
							.setMsgSettingRelations(msgSettingRelations);
		return msgNotifySettingDto;
	}
}
